<?php
/**
 * 
 * @author tangbl
 * 
 */
class BlockAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 列表
	 */
	public function index(){
		if(IS_AJAX){
			
			import('ORG.Util.Page');
			$model     = D("Block");
			
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = '1=1 '.$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			$upd       = L('cfg_list_itf_btn_modify');
			$del       = L('cfg_list_itf_btn_delete');
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'key'      => $row['key'],
						'title'    => $row['title'],
						'addtime'  => date('Y-m-d',$row['addtime']),
						'deal'=>'
					 <a href="'.__URL__.'/edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>
				     <a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
			
		}else{
			
			$this->display();
		}
	}
	
	/**
	 * 添加
	 */
	public function add(){
		$langs  =  C("LANG_ARRAY");	
		if(IS_POST){
			$param = array(
				"before"  => "Block_before",
				"reply"   => false,
				"cache"   => false,																						
			);
			parent::do_add("Block",$param);
			
		}else{
			$this->assign("langs",$langs);
			$this->display();
		}
	}
	
	/**
	 * 修改
	 */
	public function edit(){
		
		if(IS_POST){			
			$param = array(
				"before"  => "Block_before",
				"reply"   => false,
				"cache"   => false,
			);
			parent::do_edit("Block",$param);
		}else{
			
			$data   = D("Block");
			$id     = $this->_get("id","intval",0);
			$row    = $data->find($id);
			
			$value  = json_decode($row['value'],true);
			$langs  = C("LANG_ARRAY");
			foreach($langs as $k=>$v){
				$values[$k] = array("title"=>$v,"value"=>$value[$k]);
			}
		
			$this->assign("langs",$values);
			$this->assign("block",$row);
			$this->display();
		}
	}
	
	/**
	 * 缓存碎片
	 */
	public function cache(){
		
		$model = D("Block");
		$list  = $model->select();
		foreach($list as $k=>$v){
			$key   = $v['key'];
			$value = json_decode($v['value'],true);
			F("$key",$value,DATA_PATH."Block/");
		}
		wlog(1);
		$this->success("生成碎片缓存成功");
	}
	
	/**
	 * 删除
	 */
	public function del(){
		
		$param = array(
			"reply"  => false,
			"cache"  => false,
		);
		parent::do_del("Block",true,$param);
	}
}
?>